package com.kevin.ymqquan.activity;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;

import com.kevin.ymqquan.R;
import com.kevin.ymqquan.adapter.ClubActiveAdapter;
import com.kevin.ymqquan.adapter.ConClubActiveAdapter;
import com.kevin.ymqquan.application.App;
import com.kevin.ymqquan.bean.ActiveListData;
import com.kevin.ymqquan.bean.ConClubActive;
import com.kevin.ymqquan.constants.Constants;
import com.kevin.ymqquan.custom.CustomToast;
import com.kevin.ymqquan.httppost.Helper;
import com.kevin.ymqquan.httppost.HttpPostService;
import com.kevin.ymqquan.view.CommonListView;
import com.kevin.ymqquan.view.CommonListView.IXListViewListener;

public class ConCourseDActivity extends Activity implements IXListViewListener,
		OnItemClickListener {

	private ImageView iv_active_back;
	private App application;
	private boolean isRefresh = false;
	private Dialog dialog;
	private String courseId;

	private String ti;

	private List<ConClubActive> activeList = new ArrayList<ConClubActive>();
	private int currPage = 1;

	private CommonListView lv_active_list;

	ConClubActiveAdapter activeAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_club_active);
		init();

		Intent i = getIntent();
		courseId = i.getStringExtra("courseId");
		ti = i.getStringExtra("ti");

		if (Helper.checkConnection(ConCourseDActivity.this)) {
			dialog = CustomToast.createLoadingDialog(ConCourseDActivity.this,
					"数据获取中...");
			dialog.setCancelable(true);
			dialog.show();
			ActiveAsynTask policyTask = new ActiveAsynTask(true);
			policyTask.execute();

		} else {
			CustomToast.CustomActivityToast(ConCourseDActivity.this,
					"网络连接异常，请检查您的网络", -80);
		}

	}

	private void init() {
		application = (App) getApplication();
		lv_active_list = (CommonListView) findViewById(R.id.lv_club_active_list);
		iv_active_back = (ImageView) findViewById(R.id.iv_club_active_back);

		// 下拉刷新不可用
		lv_active_list.setPullLoadEnable(true);
		lv_active_list.setPullRefreshEnable(true);
		lv_active_list.setXListViewListener(this);
		lv_active_list.stopLoadMore();
		lv_active_list.stopRefresh();

		activeAdapter = new ConClubActiveAdapter(ConCourseDActivity.this,
				activeList);
		lv_active_list.setAdapter(activeAdapter);
		lv_active_list.setOnItemClickListener(ConCourseDActivity.this);

		iv_active_back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub\
				ConCourseDActivity.this.finish();
			}
		});

	}

	@Override
	public void onRefresh() {
		// TODO Auto-generated method stub
		currPage = 1;
		isRefresh = true;
		ActiveAsynTask policyTask = new ActiveAsynTask(false);
		policyTask.execute();
	}

	@Override
	public void onLoadMore() {
		// TODO Auto-generated method stub
		isRefresh = false;
		++currPage;
		ActiveAsynTask policyTask = new ActiveAsynTask(false);
		policyTask.execute();

	}

	public void clearList() {
		currPage = 1;
		activeList.clear();
		activeAdapter.notifyDataSetChanged();

	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int position,
			long arg3) {
		// TODO Auto-generated method stub
		Intent i = null;
		if (position != 0) {
			position--;
			if ("1".equals(ti)) {
				i = new Intent(ConCourseDActivity.this,
						CourseTiUserActivity.class);
			}else{
				i = new Intent(ConCourseDActivity.this,
						CourseConUserActivity.class);
			}

			i.putExtra("activeId", activeList.get(position).getActiveId() + "");
			i.putExtra("courseId", courseId + "");
			// startActivityForResult(i, position);
			startActivity(i);
		}

	}

	// @Override
	// protected void onActivityResult(int requestCode, int resultCode, Intent
	// data) {
	// // TODO Auto-generated method stub
	// // super.onActivityResult(requestCode, resultCode, data);
	// activeList.get(requestCode).setIsJoin(resultCode);
	// Log.i("kevinlog", requestCode+"----"+resultCode);
	// }

	class ActiveAsynTask extends AsyncTask<String, Object, String> {

		private boolean dialogShow = true;

		public ActiveAsynTask(boolean dialogShow) {
			// TODO Auto-generated constructor stub
			this.dialogShow = dialogShow;
		}

		@Override
		protected String doInBackground(String... params) {

			String param = "{\"method\":\"userCDList\",\"pageNum\":\""
					+ currPage + "\",\"courseId\":\"" + courseId + "\"}";

			String replyList = "";
			try {
				replyList = HttpPostService.PostJsonString(
						Constants.ServerAddr, param);
			} catch (Exception e) {
				e.printStackTrace();
			}
			// Log.i("kevinlog", "--++--" + param);
			// Log.i("kevinlog", "--++--" + replyList);
			return replyList;
		}

		@Override
		protected void onPostExecute(String JsonResult) {
			// TODO Auto-generated method stub
			super.onPostExecute(JsonResult);
			// Log.i("sssss", JsonResult);
			if (JsonResult != null) {
				// String result = JsonResult.replace("\n", "");
				String result = JsonResult;
				JSONObject jsonObject;
				try {
					jsonObject = new JSONObject(result);
					String resultType = jsonObject.getString("status");
					if ("0".equals(resultType)) {
						JSONArray jsonArray = jsonObject
								.getJSONArray("userCDList");

						if (isRefresh) {
							clearList();
						}

						for (int i = 0; i < jsonArray.length(); i++) {
							JSONObject eduPolicyObject = jsonArray
									.getJSONObject(i);
							ConClubActive eduPolicy = new ConClubActive();
							eduPolicy.setActiveId(Integer
									.parseInt(eduPolicyObject
											.getString("activeId")));

							eduPolicy.setActiveName(eduPolicyObject
									.getString("activeName"));
							eduPolicy.setActiveTime(eduPolicyObject
									.getString("activeTime"));
							eduPolicy.setIncome(eduPolicyObject
									.getString("income"));
							eduPolicy.setPay(eduPolicyObject.getString("pay"));

							activeList.add(eduPolicy);
						}

						activeAdapter.notifyDataSetChanged();
						if (dialogShow) {
							dialog.dismiss();
						}
					} else {
						if (dialogShow) {
							dialog.dismiss();
						}
						CustomToast.CustomActivityToast(
								ConCourseDActivity.this, "没有更多了", -80);
					}
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					if (dialogShow) {
						dialog.dismiss();
					}
					CustomToast.CustomActivityToast(ConCourseDActivity.this,
							"没有更多了", -80);
					e.printStackTrace();
				}
				lv_active_list.stopLoadMore();
				lv_active_list.stopRefresh();
			} else {
				if (dialogShow) {
					dialog.dismiss();
				}
				CustomToast.CustomActivityToast(ConCourseDActivity.this,
						"网络异常请稍后重试！", -80);

			}
		}
	}

}
